#include <iostream>
#include <cstdlib>
#include <cstring>
using namespace std;

struct myVoector
{
    int *pbuff;
    int len;
    myVoector():pbuff(NULL),len(0){
        cout << "无参构造函数" << endl;
    }
    myVoector(int *p,int len);
    myVoector(const myVoector&);//拷贝构造
    ~myVoector(){
        delete[] pbuff;
        cout<<"析构函数"<< endl;
    }
    void travel();//遍历
    void push(int a);//后插
    void sort();//排序

};

int main(){

    int *arr;
    myVoector a(arr,0);
    a.push(3);
    a.push(4);
    a.push(1);
    a.travel();
    a.sort();
    a.travel();

    return 0;
}

//有参构造函数
myVoector::myVoector(int *p,int len):pbuff(p),len(len){
   
    cout << "有参构造函数" << endl;
    if(len>0 && p!=NULL){
        pbuff = new int[len];
        memcpy(pbuff,p,sizeof(int)*len);
    }
    else{
        pbuff = NULL;
    }

}

//拷贝构造函数
myVoector::myVoector(const myVoector&a){
    cout << "深拷贝" << endl;
    this->len = a.len;
    if(a.pbuff != NULL){
        pbuff = new int[len];
        memcpy(pbuff,a.pbuff,sizeof(int )*len);
    }
    else{
        pbuff = NULL;
    }
}

void myVoector::travel()//遍历
{
    for(int i = 0;i<this->len;i++){
        cout << this->pbuff[i] << " ";
    }
    cout << endl;
}

void myVoector:: push(int a){
    //分配新空间
    int *pnew = new int[len+1];
    //拷贝
    if(len>0){
        memcpy(pnew,pbuff,sizeof(int)*len);
    }
    pnew[len] = a;
    //释放
    delete[] pbuff;
    pbuff = pnew;
    len++;
}

void myVoector:: sort() //排序
{
    for(int i = 0;i<len-1;i++){
        for(int j=0;j<len-i-1;j++){
            if(pbuff[j] > pbuff[j+1]){
                swap(pbuff[j],pbuff[j+1]);
            }
        }
    }
}





